#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time     :2020/11/21
# @Author   :Changshu
# @File     :Exercise_463.py
# 463. 岛屿的周长

def islandPerimeter(grid: list) -> int:
	r=0
	for i in range(len(grid)):
		for j in range(len((grid[i]))):
			count = 4
			if grid[i][j]==1:
				if j-1>=0 and grid[i][j-1]==1:
					count-=1
				if i-1>=0 and grid[i-1][j]==1:
					count-=1
				if j+1<len((grid[i])) and grid[i][j+1]==1:
					count-=1
				if i+1<len((grid)) and grid[i+1][j]==1:
					count-=1
				r+=count
	return r


if __name__ == '__main__':
	# grid=[[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
	# grid =[[1]]
	grid = [[1,1]]
	print(islandPerimeter(grid))